from strategy.StrategyTemplate import StrategyTemplate
from strategy import strategy_filter
from strategy.base_strategy.base_strategy_common import *
from utils.db import DbHandler


class DecreaseRank(StrategyTemplate):
    """
    涨幅策略:
        按跌幅排序

    """
    def __init__(self):
        super(self.__class__, self).__init__()
        self.strategy_name = "启四涨幅策略"
        self.strategy_descrption = "按跌幅排序"

    def run(self):
        # 基础过滤，过滤掉新债、强赎、EB
        self.data = strategy_filter.filter_base(self.data)
        # sort
        self.data = self.data.sort_values(by='涨跌幅', ascending=True)
        # 获得默认列
        self.data = self.data[['转债名称', '转债代码', '现价','涨跌幅', '近5日涨跌幅', '近20日涨跌幅',
                               '近3月涨跌幅', '近1年涨跌幅', '溢价率', '剩余规模', '行业',
                               '正股名称', '正股涨跌','成交额']][:self.topN]
        # 清掉原来的索引
        self.data.reset_index(inplace=True, drop=True)

    def api(self, size=10):
        db = DbHandler()
        db.create_session()
        super(self.__class__, self).api(db=db)
        self.data = self.data.sort_values(by='increase_rt', ascending=True)
        # 获得默认列
        self.data = self.data[common_column][:size]

        # 清掉原来的索引
        self.data.reset_index(inplace=True, drop=True)

        self.db.close_session()

        return self.data

if __name__ == '__main__':
    ins = DecreaseRank()
    print(ins.api())

